package com.autoplaza;

import java.text.NumberFormat;
import java.util.Locale;
import java.util.zip.DataFormatException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Resultados_Adapter extends BaseAdapter {
	private Activity activity;
	private String[] dataMarca,dataModelo,dataYear,dataVersion,dataTransmision,dataKilometraje,dataPrecio,dataLugar,
	dataImagen,dataNumFotos, dataNumAnuncio, dataColor1, dataColor2 ;
	private String[] finalDataMarca,finalDataModelo,finalDataYear,finalDataVersion,finalDataTransmision,finalDataKilometraje,finalDataPrecio,finalDataLugar,
	finalDataImagen,finalDataNumFotos, finalDataNumAnuncio, finalDataColor1, finalDataColor2 ;
	private static LayoutInflater inflater = null;
	public App_ImageLoader imageLoader;
	public int flag = 0;
	public String urlBanner = null;
//	public ImageManager imageManager;
	
	public Resultados_Adapter(Activity a, String[] marca,String[] modelo,String[] year,String[] version,
			String[] transmision,String[] kilometraje,String[] precio,String[] lugar, String[] imagen, 
			String[] numfotos, String[] numanuncio, String[] color1, String[] color2, String urlBanner) {
		int tam=marca.length;
		dataMarca=new String[tam];
		dataModelo=new String[tam];
		dataYear=new String[tam];
		dataVersion=new String[tam];
		dataTransmision=new String[tam];
		dataKilometraje=new String[tam];
		dataPrecio=new String[tam];
		dataLugar=new String[tam];
		dataImagen=new String[tam];
		dataNumFotos=new String[tam];
		dataNumAnuncio=new String[tam];
		dataColor1=new String[tam];
		dataColor2=new String[tam];

		//inicializar falta
		activity = a;
		dataMarca = marca;
		dataModelo=modelo;
		dataYear=year;
		dataVersion=version;
		dataTransmision=transmision;
		dataKilometraje=kilometraje;
		dataPrecio=precio;
		dataLugar=lugar;
		dataImagen=imagen;
		dataNumFotos=numfotos;
		dataNumAnuncio=numanuncio;
		dataColor1=color1;
		dataColor2=color2;
		this.urlBanner = urlBanner;


		//PARA SPLASH
		if(tam < 6){
			finalDataMarca = new String[tam+1];
			finalDataModelo = new String[tam+1];
			finalDataYear = new String[tam+1];
			finalDataVersion = new String[tam+1];
			finalDataTransmision = new String[tam+1];
			finalDataKilometraje = new String[tam+1];
			finalDataPrecio = new String[tam+1];
			finalDataLugar = new String[tam+1];
			finalDataImagen = new String[tam+1];
			finalDataNumFotos = new String[tam+1];
			finalDataNumAnuncio = new String[tam+1];
			finalDataColor1 = new String[tam+1];
			finalDataColor2 = new String[tam+1];

			for(int i = 0; i < finalDataMarca.length; i++){
				if(i == 0){
					finalDataMarca[i] = "splash";
					finalDataModelo[i] = "splash";
					finalDataYear[i] = "splash";
					finalDataVersion[i] = "splash";
					finalDataTransmision[i] = "splash";
					finalDataKilometraje[i] = "splash";
					finalDataPrecio[i] = "splash";
					finalDataLugar[i] = "splash";
					finalDataImagen[i] = "splash";
					finalDataNumFotos[i] = "splash";
					finalDataNumAnuncio[i] = "splash";
					finalDataColor1[i] = "splash";
					finalDataColor2[i] = "splash";

				}else{
					finalDataMarca[i] = dataMarca[i-1];
					finalDataModelo[i] = dataModelo[i-1];
					finalDataYear[i] = dataYear[i-1];
					finalDataVersion[i] = dataVersion[i-1];
					finalDataTransmision[i] = dataTransmision[i-1];
					finalDataKilometraje[i] = dataKilometraje[i-1];
					finalDataPrecio[i] = dataPrecio[i-1];
					finalDataLugar[i] = dataLugar[i-1];
					finalDataImagen[i] = dataImagen[i-1];
					finalDataNumFotos[i] = dataNumFotos[i-1];
					finalDataNumAnuncio[i] = dataNumAnuncio[i-1];
					finalDataColor1[i] = dataColor1[i-1];
					finalDataColor2[i] = dataColor2[i-1];
				}
				Log.i("finalDataMarca["+i+"]", ""+finalDataMarca[i]);
				Log.i("finalDataModelo["+i+"]", ""+finalDataModelo[i]);
				Log.i("finalDataYear["+i+"]", ""+finalDataYear[i]);
				Log.i("finalDataVersion["+i+"]", ""+finalDataVersion[i]);
				Log.i("finalDataTransmision["+i+"]", ""+finalDataTransmision[i]);
				Log.i("finalDataKilometraje["+i+"]", ""+finalDataKilometraje[i]);
				Log.i("finalDataPrecio["+i+"]", ""+finalDataPrecio[i]);
				Log.i("finalDataLugar["+i+"]", ""+finalDataLugar[i]);
				Log.i("finalDataImagen["+i+"]", ""+finalDataImagen[i]);
				Log.i("finalDataNumFotos["+i+"]", ""+finalDataNumFotos[i]);
				Log.i("finalDataNumAnuncio["+i+"]", ""+finalDataNumAnuncio[i]);
				Log.i("finalDataColor1["+i+"]", ""+finalDataColor1[i]);
				Log.i("finalDataColor2["+i+"]", ""+finalDataColor2[i]);


			}
		}else if(tam > 5){
			finalDataMarca = new String[tam+2];
			finalDataModelo = new String[tam+2];
			finalDataYear = new String[tam+2];
			finalDataVersion = new String[tam+2];
			finalDataTransmision = new String[tam+2];
			finalDataKilometraje = new String[tam+2];
			finalDataPrecio = new String[tam+2];
			finalDataLugar = new String[tam+2];
			finalDataImagen = new String[tam+2];
			finalDataNumFotos = new String[tam+2];
			finalDataNumAnuncio = new String[tam+2];
			finalDataColor1 = new String[tam+2];
			finalDataColor2 = new String[tam+2];
			for(int i = 0; i < finalDataMarca.length; i++){
				if(i == 0){
					finalDataMarca[i] = "splash";
					finalDataModelo[i] = "splash";
					finalDataYear[i] = "splash";
					finalDataVersion[i] = "splash";
					finalDataTransmision[i] = "splash";
					finalDataKilometraje[i] = "splash";
					finalDataPrecio[i] = "splash";
					finalDataLugar[i] = "splash";
					finalDataImagen[i] = "splash";
					finalDataNumFotos[i] = "splash";
					finalDataNumAnuncio[i] = "splash";
					finalDataColor1[i] = "splash";
					finalDataColor2[i] = "splash";
				}else if(i == 6){
					finalDataMarca[i] = "splash";
					finalDataModelo[i] = "splash";
					finalDataYear[i] = "splash";
					finalDataVersion[i] = "splash";
					finalDataTransmision[i] = "splash";
					finalDataKilometraje[i] = "splash";
					finalDataPrecio[i] = "splash";
					finalDataLugar[i] = "splash";
					finalDataImagen[i] = "splash";
					finalDataNumFotos[i] = "splash";
					finalDataNumAnuncio[i] = "splash";
					finalDataColor1[i] = "splash";
					finalDataColor2[i] = "splash";
				}else{
					if(i > 6){
						finalDataMarca[i] = dataMarca[i-2];
						finalDataModelo[i] = dataModelo[i-2];
						finalDataYear[i] = dataYear[i-2];
						finalDataVersion[i] = dataVersion[i-2];
						finalDataTransmision[i] = dataTransmision[i-2];
						finalDataKilometraje[i] = dataKilometraje[i-2];
						finalDataPrecio[i] = dataPrecio[i-2];
						finalDataLugar[i] = dataLugar[i-2];
						finalDataImagen[i] = dataImagen[i-2];
						finalDataNumFotos[i] = dataNumFotos[i-2];
						finalDataNumAnuncio[i] = dataNumAnuncio[i-2];
						finalDataColor1[i] = dataColor1[i-2];
						finalDataColor2[i] = dataColor2[i-2];
					}else{
						finalDataMarca[i] = dataMarca[i-1];
						finalDataModelo[i] = dataModelo[i-1];
						finalDataYear[i] = dataYear[i-1];
						finalDataVersion[i] = dataVersion[i-1];
						finalDataTransmision[i] = dataTransmision[i-1];
						finalDataKilometraje[i] = dataKilometraje[i-1];
						finalDataPrecio[i] = dataPrecio[i-1];
						finalDataLugar[i] = dataLugar[i-1];
						finalDataImagen[i] = dataImagen[i-1];
						finalDataNumFotos[i] = dataNumFotos[i-1];
						finalDataNumAnuncio[i] = dataNumAnuncio[i-1];
						finalDataColor1[i] = dataColor1[i-1];
						finalDataColor2[i] = dataColor2[i-1];
					}
				}
				Log.i("finalDataMarca["+i+"]", ""+finalDataMarca[i]);
				Log.i("finalDataModelo["+i+"]", ""+finalDataModelo[i]);
				Log.i("finalDataYear["+i+"]", ""+finalDataYear[i]);
				Log.i("finalDataVersion["+i+"]", ""+finalDataVersion[i]);
				Log.i("finalDataTransmision["+i+"]", ""+finalDataTransmision[i]);
				Log.i("finalDataKilometraje["+i+"]", ""+finalDataKilometraje[i]);
				Log.i("finalDataPrecio["+i+"]", ""+finalDataPrecio[i]);
				Log.i("finalDataLugar["+i+"]", ""+finalDataLugar[i]);
				Log.i("finalDataImagen["+i+"]", ""+finalDataImagen[i]);
				Log.i("finalDataNumFotos["+i+"]", ""+finalDataNumFotos[i]);
				Log.i("finalDataNumAnuncio["+i+"]", ""+finalDataNumAnuncio[i]);
				Log.i("finalDataColor1["+i+"]", ""+finalDataColor1[i]);
				Log.i("finalDataColor2["+i+"]", ""+finalDataColor2[i]);
			}
		}

		//******

		inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new App_ImageLoader(activity.getApplicationContext());
	}

	public int getCount() {
		//El tamaño es el mismo para cualquier arreglo, dataMarca o cualquier otro da lo mismo
		return finalDataMarca.length;
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public static class ItemHolder {
		public TextView marcamodeloyear;
		public TextView version;
		public TextView transmision;
		public TextView kilometraje;
		public TextView precio;
		public TextView lugar;
		public TextView numfotos;
		public ImageView imagen;
		public ImageView im_descr;
		public ImageView im_banner;
		public Button im_contactar;
		public LinearLayout dos, tres;
	}
	
	public static class SplashHolder{
		public WebView myWebView;
	}
	
	
	
	
	@Override
	public int getViewTypeCount() {
		return 2;
	}
	
	
	@Override
	public int getItemViewType(int position) {
		if(finalDataMarca[position].equals("splash")){
			return 0;
		}else{
			return 1;
		}
	}
	

	public View getView(final int position, View convertView, ViewGroup parent) {
		int type = getItemViewType(position);
		View finalv = null;
		Log.i("getViewAdapter", "getView " + position + " " + convertView + " type = " + type);
		switch (type) {
		case 0:
			SplashHolder splashHolder;// = new SplashHolder();
			View v1 = convertView; 
			if (v1 == null) { 
				LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v1 = inflater.inflate(R.layout.resultadosbanner, parent, false);
				splashHolder = new SplashHolder(); 
				splashHolder.myWebView = (WebView) v1.findViewById(R.id.webbanner);
//				WebSettings settings = splashHolder.myWebView.getSettings();
//				settings.setJavaScriptEnabled(true);
				splashHolder.myWebView.getSettings().setJavaScriptEnabled(true);
				splashHolder.myWebView.setBackgroundColor(Color.parseColor("#00000000"));

				splashHolder.myWebView.loadUrl(urlBanner);

//				splashHolder.myWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
				
				v1.setTag(splashHolder); 
			} 
			else { 
				splashHolder = (SplashHolder)v1.getTag(); 
			}

			//***
//			splashHolder.myWebView.getSettings().setJavaScriptEnabled(true);
//			splashHolder.myWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
//			splashHolder.myWebView.loadUrl("http://iphone.autoplaza.com.mx/BannersAndroid/Banners.aspx?");

			finalv = v1;
			break;
		case 1:
			ItemHolder itemHolder;//s = new ItemHolder();
			View v2 = convertView; 
			if(v2 == null){
				LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v2 = inflater.inflate(R.layout.resultadositems, parent, false);
				itemHolder = new ItemHolder();

				itemHolder.marcamodeloyear = (TextView) v2.findViewById(R.id.marcamodeloyear);
				itemHolder.version = (TextView) v2.findViewById(R.id.version);
				itemHolder.transmision = (TextView) v2.findViewById(R.id.transmision);
				itemHolder.kilometraje = (TextView) v2.findViewById(R.id.kilometraje);
				itemHolder.precio = (TextView) v2.findViewById(R.id.precio);
				itemHolder.lugar = (TextView) v2.findViewById(R.id.lugar);
				itemHolder.numfotos = (TextView) v2.findViewById(R.id.numfotos);
				itemHolder.imagen = (ImageView) v2.findViewById(R.id.imagencarro);
				itemHolder.im_descr = (ImageView) v2.findViewById(R.id.im_descr);
				itemHolder.im_contactar = (Button) v2.findViewById(R.id.im_contactar);
				itemHolder.dos = (LinearLayout) v2.findViewById(R.id.linearLayout2);
				itemHolder.tres = (LinearLayout) v2.findViewById(R.id.linearLayout3);

				v2.setTag(itemHolder);
			}else{
				itemHolder = (ItemHolder)v2.getTag();
			}

			//***
			//Usar arreglo[position] para esto
			NumberFormat nf = NumberFormat.getInstance();
			nf = NumberFormat.getInstance(Locale.ENGLISH);

			itemHolder.marcamodeloyear.setText(""+finalDataMarca[position]+" "+finalDataModelo[position]+" "+finalDataYear[position]);


			if(finalDataVersion[position].trim().equals("null")){
				itemHolder.kilometraje.setText("");

			}else{
				itemHolder.version.setText(""+finalDataVersion[position]);
			}


			itemHolder.transmision.setText(""+finalDataTransmision[position]);

			if(finalDataKilometraje[position].trim().equals("null")){
				itemHolder.kilometraje.setText("0 Km");

			}else{
				itemHolder.kilometraje.setText(""+nf.format(Double.parseDouble(finalDataKilometraje[position]))+" Km");
			}

			itemHolder.precio.setText("$"+nf.format(Double.parseDouble(finalDataPrecio[position])));

			itemHolder.lugar.setText(""+finalDataLugar[position]);

			itemHolder.numfotos.setText(""+finalDataNumFotos[position]);

			if(finalDataImagen[position].equals("nofoto")){
				itemHolder.imagen.setImageResource(R.drawable.sinfoto);

			}else{
				imageLoader.DisplayImage(""+finalDataImagen[position], activity,
						itemHolder.imagen);
			}

			itemHolder.dos.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent_descrp = new Intent(activity, DetalleaUTO.class);
					intent_descrp.putExtra("modelo", finalDataModelo[position]);
					intent_descrp.putExtra("numanuncio", finalDataNumAnuncio[position]);
					intent_descrp.putExtra("anio", finalDataYear[position]);
					intent_descrp.putExtra("marca", finalDataMarca[position]);
					intent_descrp.putExtra("transmision", finalDataTransmision[position]);
					intent_descrp.putExtra("precio", finalDataPrecio[position]);
					intent_descrp.putExtra("kilometraje", finalDataKilometraje[position]);
					intent_descrp.putExtra("color1", finalDataColor1[position]);
					intent_descrp.putExtra("color2", finalDataColor2[position]);
					intent_descrp.putExtra("lugar", finalDataLugar[position]);
					intent_descrp.putExtra("foto", finalDataImagen[position]);
					intent_descrp.putExtra("numfoto", finalDataNumFotos[position]);
					intent_descrp.putExtra("version", finalDataVersion[position]);
					activity.startActivity(intent_descrp);  

				}
			});

			itemHolder.tres.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent_descrp = new Intent(activity, DetalleaUTO.class);
					intent_descrp.putExtra("modelo", finalDataModelo[position]);
					intent_descrp.putExtra("numanuncio", finalDataNumAnuncio[position]);
					intent_descrp.putExtra("anio", finalDataYear[position]);
					intent_descrp.putExtra("marca", finalDataMarca[position]);
					intent_descrp.putExtra("transmision", finalDataTransmision[position]);
					intent_descrp.putExtra("precio", finalDataPrecio[position]);
					intent_descrp.putExtra("kilometraje", finalDataKilometraje[position]);
					intent_descrp.putExtra("color1", finalDataColor1[position]);
					intent_descrp.putExtra("color2", finalDataColor2[position]);
					intent_descrp.putExtra("lugar", finalDataLugar[position]);
					intent_descrp.putExtra("foto", finalDataImagen[position]);
					intent_descrp.putExtra("numfoto", finalDataNumFotos[position]);
					intent_descrp.putExtra("version", finalDataVersion[position]);
					activity.startActivity(intent_descrp);                                          
				}
			});

			itemHolder.im_contactar.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent irDatos=new Intent(activity, DatosContacto.class);
					irDatos.putExtra("from", "lista");
					irDatos.putExtra("numauncio", finalDataNumAnuncio[position]);
					activity.startActivity(irDatos);
				}
			});

			itemHolder.im_descr.setVisibility(View.GONE);


			finalv =  v2;
			break;

		default:
			break;
		}
		return finalv;
        
		
	}
}
